package com.videoaggre.zcl.ui.fragment.enterdesk

import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.Glide
import com.videoaggre.zcl.R
import com.videoaggre.zcl.bean.EnterdeskClassify
import com.videoaggre.zcl.common.*
import com.videoaggre.zcl.databinding.EnterdeskItemClassifyBinding
import com.videoaggre.zcl.ui.activity.wallpaper.enterdesk.EnterdeskClassifyImageActivity
import com.videoaggre.zcl.ui.activity.wallpaper.enterdesk.EnterdeskImageDetailActivity
import com.videoaggre.zcl.ui.adapter.BaseRecyclerAdapter
import com.videoaggre.zcl.ui.adapter.BaseViewHolder
import com.videoaggre.zcl.ui.fragment.BaseFragment
import kotlinx.android.synthetic.main.view_refresh.*

open class EnterdeskClassifyFragment : BaseFragment(R.layout.view_refresh) {
    protected val mEnterdeskClassifyAdapter = EnterdeskClassifyAdapter()
    private var islLoad = false
    override fun onResume() {
        super.onResume()
        if (!islLoad) {
            islLoad = true
            onReload()
        }
    }

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        refreshLayout.apply {
            setBackgroundColor(R.color.white.toColor())
            setEnableRefresh(false)
            setEnableLoadMore(false)
        }
        mEnterdeskClassifyAdapter.setOnClickAdapterListenter(::onClick)
        recyclerView.adapter = mEnterdeskClassifyAdapter
        recyclerView.layoutManager = GridLayoutManager(context, 2)
        recyclerView.setPadding(3.dp, (-3).dp, 3.dp, 3.dp)
    }

    override fun initData(rootView: View?, savedInstanceState: Bundle?) {
        mEnterdeskClassifyAdapter.datas = mutableListOf<EnterdeskClassify>().apply {
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/ac/2a/5c/ac2a5c410706ca28ded3e3abf14ed480.jpg", "美女", "woman"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/81/b4/1a/81b41a13b09b1814737f67c6bab7e1ea.jpg", "植物", "zhiwu"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/d3/69/cb/d369cbdb8fcc3c0dcbb36e1b7b9b5818.jpg", "动漫", "anime"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/fa/83/52/fa8352810355208bdac1a9d48488c840.jpg", "食物", "food"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/86/ee/0f/86ee0f73da7d2f48981340b351c54b22.jpg", "影视", "dianyingdianshi"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/c8/6d/11/c86d11ccda86f514abd8256ea6e41691.jpg", "非主流", "feizhuliushouji"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/2c/11/d1/2c11d1139f21ab253c80e12c4dcb69c0.jpg", "明星", "mingxinggeshou"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/4e/e4/3d/4ee43db5b101a920235cbe0716313bb4.jpg", "军事", "junshiwuqi"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/a8/6e/4c/a86e4c842c6f8260978f1b1a8a54253d.jpg", "游戏", "youximanhua"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/f7/85/96/f7859692b6d5bc462ad20fa4101c67f7.jpg", "汽车", "qichejiaotong"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/41/11/87/41118719d1f034dfa6fb589fcb8d34ea.jpg", "爱情", "langmangaiqing"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/86/26/f0/8626f0e74b4e150413a8dac8b6df0046.jpeg", "体育", "yundongshouji"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/b5/c4/85/b5c4857a8b1f5d846598d9eb795f0d71.jpg", "搞笑", "gaoxiaotupian"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/f2/22/ac/f222acee0112fc7eac90a5f9a584fefe.jpg", "动物", "dongwushijie"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/c6/56/f1/c656f1d1a3395162d9f67eb1996bc7b1.jpg", "风景", "yishufengjing"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/3d/61/91/3d61917dec24943bb20ada7fa85c9a25.jpg", "节日", "jierizhufu"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/f6/40/ce/f640ced7820c139e2bb4b1eb9a6515d8.jpg", "标志", "biaozhi"))
            add(EnterdeskClassify("https://up.enterdesk.com/edpic_360_360/f1/1f/01/f11f019f866528d183f8cd18a56a6383.png", "另类", "lingleitupian"))
        }
        mEnterdeskClassifyAdapter.notifyDataSetChanged()
    }

    open fun onClick(view: View, position: Int) {
        startKtxActivity<EnterdeskClassifyImageActivity>(value = ENTERDESK_CLASSIFY to mEnterdeskClassifyAdapter.getItem(position))
    }

    private fun onReload() {
//        https://up.enterdesk.com/edpic_360_360/ac/2a/5c/ac2a5c410706ca28ded3e3abf14ed480.jpg
    }

}

class EnterdeskClassifyAdapter : BaseRecyclerAdapter<BaseViewHolder<EnterdeskItemClassifyBinding>, EnterdeskClassify>() {
    override fun onCreateViewHolder(parent: ViewGroup, p1: Int) = BaseViewHolder(EnterdeskItemClassifyBinding.inflate(getLayoutInflate(parent.context), parent, false))
    override fun onBindViewHolder(holder: BaseViewHolder<EnterdeskItemClassifyBinding>, bean: EnterdeskClassify, position: Int) {
        holder.mViewBinding.apply {
            bean.apply {
                tvClassifyName.text = name
                Glide.with(ivImage.context)
                    .load(img)
//                        .transition(TransitionOptions())
                    .transition(GenericTransitionOptions.with(R.anim.zoom_in))
                    //.transition(DrawableTransitionOptions.withCrossFade(1000))
                    .into(ivImage)
                setViewClick(position, root)
            }
        }
    }
}